package com.thinkingstar.iads.test.urule.controller;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.GET;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.bstek.urule.runtime.KnowledgeSession;
import com.bstek.urule.runtime.response.RuleExecutionResponse;
import com.thinkingstar.iads.test.urule.action.HighspeedrailAction;
import com.thinkingstar.iads.test.urule.entity.UserPoJo;

/**
 * Desc
 * http://localhost:8080/rule/getHig/定员
 * @author zengxzh@yonyou.com
 * @version V1.0.0
 * @date 2018/1/4
 */
@At("/rule")
@IocBean
public class RuleController {

	@Inject
	protected PropertiesProxy conf;

	@Inject
	protected Dao dao;

	@Inject
	protected HighspeedrailAction highspeedrailAction;

	
	@At("/getHig")
	@Ok("json:full")
	public List<Map<String,Object>> getHig(@Param("name") String name) throws IOException {
		//创建一个KnowledgeSession对象， 项目名/知识包ID
		KnowledgeSession session = RuleTool.getSessionFactory( "test01/hig");
		Map<String, Object> param = new HashMap<>();
		param.put("mechanismType", name);
		//执行规则流,填写决策流ID 和 赋值的参数
		session.startProcess("demo",param);
		return  HighspeedrailAction.list;
	}

	@At("/getInputData/?/?")
	@Ok("json:full")
	public List<Map<String,Object>> getInputData(String eng_id,String name) {
		return highspeedrailAction.getInputData(eng_id, name);
	}

	@At("/cot/?/?")
	@Ok("json:full")
	public Map<String,Object> rule( String name, int cot) throws IOException {
		//创建一个KnowledgeSession对象
		KnowledgeSession session = RuleTool.getSessionFactory( "test01/ceshi");
		Map<String, Object> param = new HashMap();
		param.put("cot", cot);
		param.put("name", name);
		//执行规则, 赋值的参数
		RuleExecutionResponse fireRules = session.fireRules(param);
		//获取参数.pl.xml文件的某个参数，强转即可
		List<Map> temporaryList = (List<Map>) session.getParameter("temporaryList");
		List<Map> list = (List<Map>) session.getParameter("list");
		param.clear();
		param.put("rule1", temporaryList);
		param.put("rule2", list);
		return param;
	}


	@At("/get")
	@Ok("json:full")
	@GET
	public List<UserPoJo> getInputData() {
		List<UserPoJo> list = new ArrayList<UserPoJo>();
		Sql sql = Sqls.create("select t.loginname,t.realname, u.name "
				+"as unitname,t.id from SYS_USER t left join SYS_UNIT u on t.unitid=u.id");
		sql.setCallback(new SqlCallback(){
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				while (rs.next()) {
					UserPoJo m = dao.getEntity(UserPoJo.class).getObject(rs, null, "");
					m.setShowname(m.getUnitname()+"_"+m.getRealname());
					m.setShowvalue(m.getId()+"_"+m.getLoginname());
					list.add(m);
				}
				return list;
			}

		});
		dao.execute(sql);
		return list;
	}

}
